package com.zhangdi.demo.es.thread;

import com.github.pagehelper.PageHelper;
import com.iowl.search.dto.*;
import com.zhangdi.demo.es.entity.CompanyDoc;
import com.zhangdi.demo.es.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by zhangdi on 2020/07/10 14:58:18.
 */

@Service
public class ThreadBoss {
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    CompanyProjectMapper projectMapper;
    @Autowired
    CompanyEngineerMapper engineerMapper;
    @Autowired
    CompanyProjectWinbiddingMapper winbiddingMapper;
    @Autowired
    CompanyQualificationMapper qualificationMapper;
    @Autowired
    CompanyRecordMapper recordMapper;

    @Autowired
    ThreadWorker threadWorker;

    private int no = 1;
    private int size = 1000;

    @Async("taskExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void run() {
        System.out.println("开始----" + new Date());
        while (true) {
            //读业务
            PageHelper.startPage(no, size);
            List<CompanyDoc> companyList = companyMapper.findAll();
            if (companyList.size() == 0) {
                System.out.println("已经传输完毕.");
                break;
            }

            for (CompanyDoc company : companyList) {
                //备案处理
                company.setRecords(recordMapper.findRecordAreaIdsByCompanyId(company.getId()));

                //企业人员证书数量
                List<CompanyErMajorInfo> erMajorInfoList = engineerMapper.companyEngineerMajorCount(company.getId());
                company.setMajorList(erMajorInfoList);

                //查询业绩
                List<CompanyProject> projects = projectMapper.findByCompanyId(company.getId());
                for (CompanyProject project : projects) {
                    company.addProject(project);
                }
                List<CompanyProjectWinbidding> winbiddings = winbiddingMapper.findByCompanyId(company.getId());
                for (CompanyProjectWinbidding winbidding : winbiddings) {
                    company.addZB(winbidding);
                }

                //人员
                company.setEngineerList(engineerMapper.findByCompanyId(company.getId()));

                //资质
                company.setZzList(qualificationMapper.findByCompanyId(company.getId()));

//                outList.add(company);
            }
            ++no;
            threadWorker.run(companyList);
        }

        System.out.println("调用完成" + new Date());
    }
}